<?php session_start();?>
<!DOCTYPE HTML/>
<html>
	<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
	<head>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
		<?php include "include.php"?>
	</head>
	<body>
		<?php include "header.php";
			include "connect.php";
			
			$query="SELECT * FROM post ORDER BY no DESC";
			$result=mysql_query($query);
			$totalPost=mysql_num_rows($result);
		?>
		
		<div class="container">
			<div class="text-center">
				<h1>My Download</h1>
				내가 Exam, Resource 에서 다운로드 한 자료를 확인할 수 있습니다.<br/>
			</div>
			<h3><i class="icon-book"></i> Exam</h3>
			<br/>
			<table class="table table-hover table-center">
				<tr>
					<th>과목명</th>
					<th>교수님</th>
					<th>분류</th>
					<th>좋아요</th>
					<th>파일</th>
				</tr>
			<?php 
				for($i=0; $i<$totalPost; $i++){
					mysql_data_seek($result, $i);
					$row=mysql_fetch_array($result);
					$time=strtok($row[time], " ");
					echo '
					<tr>
						<td>'.$row[title].'</td>
						<td>'.$row[writer].'</td>
						<td>2013년 가을 기말고사</td>
						<td><i class="icon-thumbs-up-alt"></i> '.$row[hit].'</td>	
						<td><span class="badge badge-important">pdf</span></td>	
					</tr>
					';
				}
			?>
			</table>

			<?php 		
				$postPerPage=10;
				$page=$_GET['page'];
				$firstPost=$_GET['first'];
				
				$query="SELECT * FROM post ORDER BY no ASC";
				$result=mysql_query($query);
				$totalPost=mysql_num_rows($result);
				
				$totalPage=ceil($totalPost/$postPerPage);
				
				if($page=='') $page=1;
				else if($page<1 || $page>$totalPage) 
					echo "
						<script>
							alert('Invalid Access.');
							history.go(-1);
						</script>
					";
				
				if($firstPost=='') $firstPost=$totalPost-(($page-1)*$postPerPage);
			?>

			<h3><i class="icon-book"></i> Resource</h3>
			<br/>
			<table class="table table-hover">
			<?php 
				for($i=$firstPost-1;$i>=0 && $i>($firstPost-$postPerPage-1);$i--){
					mysql_data_seek($result, $i);
					$row=mysql_fetch_array($result);
					$time=strtok($row[time], " ");
					
					echo '
					  <tr>
					  	<td><span class="muted"><small>'.$row[no].'</small></span><font class="crimsonText"> <i class="icon-caret-right"></i><strong><a type="button" data-toggle="collapse" data-target="#resource1" rel="tooltip" data-placement="bottom"> '.$row[title].'</a></strong></font></td>
						<td><span class="pull-right">(data.zip, 305MB)<span class="badge badge-inverse"><i class="icon-arrow-down"></i> 다운로드</span></span></td>
				        <td><span class="crimsonLink pull-right">
						          <a href="#">'.$row[writer].'</a> 
						          | '.$time.'
						          | <i class="icon-thumbs-up-alt"></i> <a href="#">'.$row[hit].' </a>
						    </span></td>
					  </tr>			
					';
				}
			?>
			</table>	
			
			<div class="pagination pagination-centered">
				<ul>
					<?php 
						$print='';
							
						for($i=1;$i<=$totalPage;$i++){							
							if($page==$i) $print=$print."<li><span class='crimsonText'><a href='qna.php?page=$i'>$i</a></span></li>";
							else $print=$print."<li><span class='crimsonLink'><a href='resource.php?page=$i'>$i</a></span></li>";
						}

						echo $print;
					?>		
				</ul>
			</div>			
      </div>				
	<script src="js/menu.js"></script>
	</body>
</html>